package com.cmd.driver;

import java.net.InetSocketAddress;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.mgr.UserOrderMgr;
import com.mgr.UserPayMgr;
import com.player.CommonUser;
import com.player.DriverUser;
import com.player.User;
import com.player.WorldMgr;
import com.protocol.Protocol;
import com.qy.bean.OrderDriver;
import com.qy.bean.OrderInfo;
import com.qy.bean.PayInfo;
import com.qy.util.StaticData;
import com.qy.util.TimeUtil;
import com.util.AbstractCommand;
import com.util.PBMessage;
import com.util.QinYiWXPay;

/**
 * 
 * 取消订单
 * 
 * @author zwl 2018年2月1日
 */
public class OrderCancelCMD extends AbstractCommand {

    @Override
    public void execute(User user, PBMessage packet) throws Exception {
	/*
	 * 1.如果是还未接单，则直接取消，
	 * 2.如果是有人接单了或者是接了单还没到达，
	 *   距离用户取车两小时外扣除金额的5%
	 *   距离用户取车两小时内扣除金额的10%
	 *   取消司机的服务号
	 *   取消订单
	 *   取消支付订单
	 *   退款
	 */
	DriverUser driverUser = (DriverUser) user;;
	JSONObject reJs = new JSONObject();
	reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
	JSONObject param = packet.getMessage();
	if (!param.containsKey("orderId")) {
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.param_error.getType());
	    user.sendPacket(reJs);
	    return;
	}
	// 订单号
	int orderId = param.getInteger("orderId");
	OrderInfo info = UserOrderMgr.getOrderInfo(orderId);
	if (info == null
		||info.getOrderStatus() != StaticData.OrderStatus.ARRIVE_NOT_CHECK.getType()
		||info.getOrderStatus() !=StaticData.OrderStatus.ACCEPT_NOT_CHECK.getType() ) {
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ORDER_not_exist.getType());
	    user.sendPacket(reJs);
	    return;
	}
	/*
	 * 2取消后，司机取消订单，车主提示订单已取消，直接重新发布。
	 * 3.倒计时未结束，不可取消，结束后，司机可点取消，获得100元。
	 * 显示在已完成订单内。状态为，已完成。 车主订单变为，已取消。
	 * 8。司机可取消，订单返回车检员，变成状态7
	 */
	if(info.getOrderStatus() ==StaticData.OrderStatus.ACCEPT_NOT_CHECK.getType() ){
	    info.setOrderStatus(StaticData.OrderStatus.COMPLET_CHECK_NOT_ACCEPT.getType());
	    UserOrderMgr.update(info);
//	}else if(info.getOrderStatus() == StaticData.OrderStatus.ACCEPT_NOT_ARRIVE.getType()){
//	    info.setOrderStatus(StaticData.OrderStatus.PAY_NOT_ACCEPT.getType());
//	    UserOrderMgr.update(info);
	} else{
	    PayInfo payInfo = UserPayMgr.getPay(info.getPayId());
	    if (payInfo == null
		    || payInfo.getPayStatus() != StaticData.PayInfoStatus.FINISH.getType()) {
		    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.PAY_not_exist.getType());
		    user.sendPacket(reJs);
		    return;
	    }
		/*
		 * 获取司机的服务订单
		 * 如果倒计时已经过了的话则可以取消
		 * 给司机25%的钱
		 */
		int reduce = 0;
		QinYiWXPay qyp = null;
		OrderDriver od = driverUser.getServerOrder();
		if (od == null||od.getCountDownTime()==null
			||TimeUtil.getSysteCurTime().before(od.getCountDownTime())) {
			reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ORDER_not_exist.getType());
			user.sendPacket(reJs);
			return;
		}
		 
		 synchronized (this) {
			/*
			 * 扣25%
			 */
			reduce = payInfo.getOrderPrice() * 25 / 100;
			if (reduce > 0) {
			    driverUser.getDriverInfo().setMoney(driverUser.getDriverInfo().getMoney() + reduce);
			}
		    }
		    qyp = new QinYiWXPay(payInfo.getOrderPrice(),
			    ((InetSocketAddress) user.getUserChannel().remoteAddress()).getAddress().getHostAddress(),
			    user.getOpenId(),
			    payInfo.getOrderNum());
		    Map<String, String> payParam = qyp.doRefund((payInfo.getOrderPrice() - reduce) + "");
			if (payParam.get("return_code").equals("FAIL")) {
			    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.CANCEL_ERROR.getType());
			    reJs.put("err_msg", payParam.get("err_msg"));
			    user.sendPacket(reJs);
			    return;
			}
			if (payParam.get("return_code").equals("FAIL")) {
			    reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.CANCEL_ERROR.getType());
			    reJs.put("err_msg", payParam.get("err_msg"));
			    user.sendPacket(reJs);
			    return;
			}
			payInfo.setPayStatus(StaticData.PayInfoStatus.REFUND.getType());
			UserPayMgr.update(payInfo);
			
			info.setOrderStatus(StaticData.OrderStatus.CANCEL.getType());
			UserOrderMgr.update(info);
		    /*
		     * 服务号取消
		     */
		    od.setStatus(StaticData.DriverOrderStatus.END.getType());
		    driverUser.setServerOrder(null);
		    
		    CommonUser cUser = (CommonUser) WorldMgr.getGamePlayer(info.getId(),
			    StaticData.userType.COMMON_USER.getType());
		    JSONObject dReJs = new JSONObject();
		    dReJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ORDER_CANCEL.getType());
		    dReJs.put(StaticData.reKey.cmd.getKey(), Protocol.U_ORDER_CANCEL);
		    cUser.sendPacket(dReJs);
		}

	reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ok.getType());
	user.sendPacket(reJs);
	return;
    }

}
